/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.beans;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import net.martinimix.beans.bind.support.CurrencyBasePairPropertyEditor;
import net.martinimix.beans.bind.support.DNABasePairPropertyEditor;
import net.martinimix.beans.bind.support.DoubleBasePairPropertyEditor;
import net.martinimix.beans.bind.support.IntegerArrayBasePairPropertyEditor;
import net.martinimix.beans.bind.support.IntegerBasePairPropertyEditor;
import net.martinimix.beans.bind.support.PhoneNumberBasePairPropertyEditor;
import net.martinimix.domain.account.PhoneNumber;

import com.bluemartini.dna.DNABasePair;
import com.bluemartini.dna.DNAIntegerArray;

/**
 * Provides a DNA base pair property editor registry for supported DNA base pair types.
 * 
 * <p>
 * FIXME: not all base pair types are currently supported.
 * </p>
 * 
 * @author Scott Rossillo
 *
 */
public final class MartiniMixBasePairPropertyEditorRegistry implements
		BasePairPropertyEditorRegistry {
	
	private final Map classTypeMap = new HashMap();
	private final Map basePairTypeMap = new HashMap();
	
	/**
	 * Creates a new property editor registry.
	 */
	public MartiniMixBasePairPropertyEditorRegistry() {
		
		// TODO: externalize initialization?
		this.registerCustomEditor(
				BigDecimal.class, DNABasePair.TYPE_CURRENCY, new CurrencyBasePairPropertyEditor());
		
		this.registerCustomEditor(
				Double.class, DNABasePair.TYPE_DOUBLE, new DoubleBasePairPropertyEditor());
		
		this.registerCustomEditor(
				Integer.class, DNABasePair.TYPE_INTEGER, new IntegerBasePairPropertyEditor());
		
		// FIXME: we need list support
		this.registerCustomEditor(
				DNAIntegerArray.class, DNABasePair.TYPE_INTEGER_ARRAY, new IntegerArrayBasePairPropertyEditor());
		
		this.registerCustomEditor(
				PhoneNumber.class, DNABasePair.TYPE_STRING, new PhoneNumberBasePairPropertyEditor());
	}

	/* (non-Javadoc)
	 * @see net.martinimix.beans.BasePairPropertyEditorRegistry#findCustomEditor(java.lang.Class)
	 */
	public DNABasePairPropertyEditor findCustomEditor(Class requiredType) {
		return (DNABasePairPropertyEditor) classTypeMap.get(requiredType);
	}

	/* (non-Javadoc)
	 * @see net.martinimix.beans.BasePairPropertyEditorRegistry#findCustomEditor(int)
	 */
	public DNABasePairPropertyEditor findCustomEditor(int requiredBasePairType) {
		return (DNABasePairPropertyEditor) basePairTypeMap.get(new Integer(requiredBasePairType));
	}

	/* (non-Javadoc)
	 * @see net.martinimix.beans.BasePairPropertyEditorRegistry#registerCustomEditor(java.lang.Class, int, net.martinimix.dao.bind.support.DNABasePairPropertyEditor)
	 */
	public void registerCustomEditor(Class requiredType,
			int requiredBasePairType, DNABasePairPropertyEditor propertyEditor) {
		
		classTypeMap.put(requiredType, propertyEditor);
		basePairTypeMap.put(new Integer(requiredBasePairType), propertyEditor);
	}

}
